-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Highlight Rust String interpolation macros #12768
base: master
Are you sure you want to change the base?
Conversation
runtime/queries/rust/injections.scm
Outdated
@@ -75,3 +75,7 @@ | |||
[(string_literal) (raw_string_literal)] @injection.content | |||
) | |||
(#set! injection.language "sql")) | |||
|
|||
; TODO: change this so it injects into all strings within a macro call | |||
((string_content) @injection.content |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to be smarter here and match on the macro name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I think just injecting it for stdlib and a few known crate macros (e.g. slog
and tracing
macros) would be a huge improvement already, and then you could always add more crates over time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following query should inject the language into the 1st argument, but it doesn't work due to a bug in tree-sitter which has been maybe fixed in v0.25
((macro_invocation
macro:
[
(scoped_identifier
name: (_) @_macro_name)
(identifier) @_macro_name
]
(token_tree . (string_literal) @injection.content))
(#eq? @_macro_name "format_args")
(#set! injection.language "rustfmt")
(#set! injection.include-children))
For now, i made this PR such that it injects the rustfmt
parser into every argument of these macros:
(#any-of? @_macro_name
; std
"format"
"write"
"writeln"
"print"
"println"
"eprint"
"eprintln"
"format_args"
; log
"crit"
"error"
"warn"
"info"
"debug"
"trace"
; anyhow
"anyhow"
"bail"
"ensure")
Once Helix updates to Tree-Sitter v0.25, we can make a new PR that will inject only into positional arguments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we want to wait for the new tree-sitter bindings and highlighter for this (#10286). I'm pretty sure the issue with the query you mentioned is that there isn't a way to control precedence of injections and the new highlighter will fix that.
name: (_) @_macro_name) | ||
(identifier) @_macro_name | ||
] | ||
(token_tree) @injection.content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than injecting into the whole token tree this should be targetting specifically (string_contents)
nodes, and not setting injection.include-children
. The rustfmt
part shouldn't need to handle escapes since the Rust parser already does that (plus it handles all cases like unicode, e.g. \u{20FF}
), and rustfmt doesn't have enough information to know whether escapes should be highlighted or not - they shouldn't be inside raw string literals (r#"foo"#
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than injecting into the whole token tree this should be targetting specifically
(string_contents)
nodes, and not settinginjection.include-children
. Therustfmt
part shouldn't need to handle escapes since the Rust parser already does that (plus it handles all cases like unicode, e.g.\u{20FF}
), and rustfmt doesn't have enough information to know whether escapes should be highlighted or not - they shouldn't be inside raw string literals (r#"foo"#
).
(escaped)
is only responsible for detecting escaped {{
and }}
, for example format!("{{ hello }}")
will evaluate to "{ hello }"
. Since those aren't covered by rust's parser
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried setting string_contents
using a variety of ways:
Before:
(token_tree) @injection.content)
I removed the injection.include-children
and then tried changing it to all of:
(string_contents) @injection.content)
(token_tree (string_contents) @injection.content))
(token_tree (string_literal (string_contents) @injection.content)))
(token_tree (string_contents)) @injection.content)
But in each case Rust syntax highlighting turns off. is it also related to #10286 ?
Encompassing the 2 feedback threads, would these changes/fixes also open up SQL being injected into |
yes, injecting any other language into the macros should be easy to base off this PR (once it actually works, when the problems are fixed) |
Hi, so I made a tree sitter grammar for rust
format_args!
which improves the Helix Rust experience, first of all by making it look prettier and easier to tell text apart from actual syntaxparser repo:
tree-sitter-rustfmt
: https://github.com/nik-rev/tree-sitter-rustfmtHere's how it looks like:
Before
After
Notes
At the moment the PR injects this language into every string. What should be done instead is that it's injected into every string within a macro's arguments. I tried to do this, I'm not sure how to exactly. This was my best attempt, but it completely turned off syntax highlighting when I tried it:
(macro_invocation (string_content) @injection.content (#set! injection.language "rustfmt"))
Closes #5845